package com.zjl.controller;


import com.zjl.anno.Log;
import com.zjl.pojo.Emp;
import com.zjl.pojo.PageBean;
import com.zjl.pojo.Result;
import com.zjl.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @ClassName EmpController
 * @Description TODO
 * @Author ZJL
 * @Date 2023/10/26 18:21
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 条件分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Log
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        //日志
        log.info("条件分页参数：参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);

        //调用service分页查询 获取PageBean
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);

        return Result.success(pageBean);
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作，ids：{}", ids);
        Integer result = empService.delete(ids);
        if (result > 0) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工，emp：{}", emp);
        Integer result = empService.add(emp);
        if (result>0){
            return Result.success();
        }else{
            return Result.error("新增失败");
        }
    }

    @Log
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询员工，id：{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工，emp：{}", emp);
        Integer result = empService.update(emp);
        if (result>0){
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }


}
